package com.example.memo;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.memo.Adapter.NoteAdapter;
import com.example.memo.db.NoteHelper;
import com.example.memo.db.UserHelper;
import com.example.memo.entity.NoteInfo;
import com.example.memo.entity.UserInfo;

import java.util.List;

public class HomeFragment extends Fragment implements View.OnClickListener {
    private View rootView;
    private CardView create;
    private RecyclerView recyclerView;
    private List<NoteInfo> noteList;
    private NoteAdapter noteAdapter;
    private NoteHelper noteHelper;
    private ActivityResultLauncher activityResultLauncher;
    private UserInfo userInfo;
    private String[] item1 = new String[]{"置顶","编辑","删除"};
    private String[] item2 = new String[]{"取消置顶","编辑","删除"};
    private int currentChoice;
    private RecyclerView.LayoutManager layoutManager;

    public HomeFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_home, container, false);
        initView(rootView);
        initData();
        return rootView;
    }

    public void initView(View rootView){
        create = rootView.findViewById(R.id.create);
        recyclerView = rootView.findViewById(R.id.recyclerview);
        noteAdapter = new NoteAdapter();
        recyclerView.setAdapter(noteAdapter);
        noteHelper = new NoteHelper(getActivity());

        activityResultLauncher = this.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                o -> {
                    noteList = noteHelper.queryNoteList(userInfo.getAccount());
                    noteAdapter.addNote(noteList);
                });

        create.setOnClickListener(this);
        noteAdapter.setOnItemClickListener(new NoteAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(),NotePage.class);
                intent.putExtra("noteInfo",noteList.get(position));
                startActivity(intent);
            }

            @Override
            public void onMore(int position) {
                currentChoice = 0;
                new AlertDialog.Builder(getActivity())
                        .setTitle("操作")
                        .setNegativeButton("取消",null)
                        .setSingleChoiceItems(noteList.get(position).getIstop() == 0 ? item1:item2, 0, (dialogInterface, i) -> currentChoice = i)
                        .setPositiveButton("确认", (dialogInterface, i) -> {
                            if(currentChoice == 0){
//                                    置顶
                                noteList.get(position).setIstop(noteList.get(position).getIstop() == 0 ? 1:0);
                                noteHelper.setTop(noteList.get(position).getNoteid(),noteList.get(position).getIstop());
                                noteAdapter.addNote(noteList);
                            } else if (currentChoice == 1) {
//                                    修改
                                Intent intent = new Intent(getActivity(),CreatePage.class);
                                intent.putExtra("noteInfo",noteList.get(position));
                                activityResultLauncher.launch(intent);
                            } else if (currentChoice == 2) {
//                                    删除
                                noteHelper.deleteNote(noteList.get(position).getNoteid());
                                noteList = noteHelper.queryNoteList(userInfo.getAccount());
                                noteAdapter.addNote(noteList);
                            }
                        })
                        .show();
            }
        });
    }

    private void initData() {
        userInfo = UserInfo.getUserInfo();
        noteList = noteHelper.queryNoteList(userInfo.getAccount());
        noteAdapter.addNote(noteList);

        layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void onClick(View view) {
        if(view == create){
            activityResultLauncher.launch(new Intent(getActivity(),CreatePage.class));
        }
    }
}